package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template45 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();
//        Color color1 = new Color(192, 0, 0);

        List<String> dateList = getDateList(data);
//        List<Integer> redRows = getRedRows(data);

        //遍历获取每一个单元格
        for (int row = 0; row<numberOfRows; row++ ){
            if (row == 1){
                continue;
            }
            for (int col =0 ;col < numberOfColumns; col++){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){
                        XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
                        if (row ==0){
                            if (col ==0){
                                continue;
                            }
                            if (col > 7){
                                continue;
                            }
                            String s = dateList.get(col - 1);
                            textRun.setText(s);
                            continue;
                        }
                        if (col == 0){
                            textRun.setText(getRowName(row));
                            continue;
                        }
                        textRun.setText(getCesllData(data,row,col));
                    }
                }
            }
        }
    }

    private String getCesllData(JsonNode data, int row, int col) {
        String rowName = getRowName(row);
        double result = 0;
        for (JsonNode node : data.get("data")) {
            if (node.get("name").asText().equals(rowName)){
                if (col < 8){
//                    System.out.println(col);
                    result = node.get("tempPrds").get(col-1).get("count").asDouble();
                    break;
                }
                if (col == 8){
                    result = node.get("total").asDouble();
                    break;
                }
//                if (col == 9){
//                    result = node.get("remaining").asDouble();
//                    break;
//                }
//                if (col == 10){
//                    return node.get("rate").asText();
//                }
            }
        }
        return result == 0 ? "-" : StringUtil.decimalFormat(result);
    }

    private List<String> getDateList(JsonNode jsonNode){
        List<String> list = new ArrayList<>();
        JsonNode jsonNode1 = jsonNode.get("data").get(0).get("tempPrds");
        for (JsonNode node : jsonNode1) {
            list.add(node.get("date").asText());
        }
        return list;
    }

//    private List<Integer> getRedRows(JsonNode jsonNode) {
//
//        List<Integer> list = new ArrayList<>();
//
//        Page45 deserialize = JsonUtil.deserialize(JsonUtil.serialize(jsonNode), Page45.class);
//        List<TempData45> data = deserialize.getData();
//
//        List<TempData45> tempData45s = data.stream().filter(been ->
//                !(been.getName().equals("腾讯普通会员月卡")
//                        || been.getName().equals("腾讯高级会员月卡")
//                        || been.getName().equals("腾讯高级会员季卡")
//                        || been.getName().equals("威尔仕健身单周卡+私教体验课")
//                        || been.getName().equals("威尔仕单周卡")
//                        || been.getName().equals("威尔仕健身双周卡")))
//                .sorted(Comparator.comparing(been1 ->
//                        Double.parseDouble(been1.getRate().replace(",","").replace("%", ""))))
//                .collect(Collectors.toList());
//
//        System.out.println("tempData45s:"+JsonUtil.serialize(tempData45s));
//
////        Collections.reverse(tempData45s);
//        for (int i = tempData45s.size()-1; i > 5; i--) {
//            list.add(getRow(tempData45s.get(i).getName()));
//        }
////        for (TempData45 data45 : tempData45s) {
////            list.add(getRow(data45.getName()));
////        }
//        //guding tengxunka
//        list.addAll(Arrays.asList(13,14,15));
//        return list;
//    }

    private static String getRowName(int row){
        List<String> list = Arrays.asList("小米手环5", "NIKE健身包", "云麦智能跳绳",
                "Keep智能体脂秤电子秤", "Keep健腹轮", "Keep泡沫轴",
                "Keep手机臂包", "NIKE J足球护腿板（1 对）", "Keep俯卧撑支架",
                "迪卡侬沙袋", "Keep旗舰店瑜伽砖", "准者膝盖冰敷贴（四片装）");
        return list.get(row-2);
    }

    private static int getRow(String name){
        switch (name){
            case "小米手环5":
                return 2;
            case "NIKE健身包":
                return 3;
            case "云麦智能跳绳":
                return 4;
            case "Keep智能体脂秤电子秤":
                return 5;
            case "Keep健腹轮":
                return 6;
            case "Keep泡沫轴":
                return 7;
            case "Keep手机臂包":
                return 8;
            case "NIKE J足球护腿板（1 对）":
                return 9;
            case "Keep俯卧撑支架":
                return 10;
            case "迪卡侬沙袋":
                return 11;
            case "Keep旗舰店瑜伽砖":
                return 12;
            case "准者膝盖冰敷贴（四片装）":
                return 13;
//            case "腾讯高级会员季卡":
//                return 15;
//            case "威尔仕健身单周卡+私教体验课":
//                return 16;
//            case "威尔仕单周卡":
//                return 17;
//            case "威尔仕健身双周卡":
//                return 18;
            default:
                return 99;
        }
    }

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("45KEY-1")){
                        String summary = data.get("title").asText();
                        templateUtil.text(textParagraph,summary,false);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text error :{}",45,e);
                }
            }
        }
    }

    //rongyu
    @Data
    private static class TempPrd{
        private String date;
        private long count;
    }

    @Data
    private static class TempData45{
        private String name;
        private List<TempPrd> tempPrds;
        private double total;
        private double remaining;
        private String rate;
    }

    @Data
    private static class Page45{
        private String title;
        private List<TempData45> data;
    }
}
